﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Du_Lich
{
    public partial class TuyenDungDaoTaoForm : Form
    {
        TuyenDung td = new TuyenDung();
        public TuyenDungDaoTaoForm()
        {
            InitializeComponent();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            td.ThemNVTuyenDung(txtMaNV.Text, txtHoTen.Text, cbPhai.Text, dateTimePicker1.Value.ToString(), txtDiaChi.Text, txtQuanHuyen.Text, cbTrinhDo.Text, cblopdaotao.Text);
            gvTuyenDungDaoTao.DataSource=td.ThongTinNhanVien();
            td.Rong(txtMaNV.Text, txtHoTen.Text, cbPhai.Text, txtDiaChi.Text, txtQuanHuyen.Text, cbTrinhDo.Text, cblopdaotao.Text);
        }

        private void TuyenDungDaoTaoForm_Load(object sender, EventArgs e)
        {
            td.MoKetNoi();
            gvTuyenDungDaoTao.DataSource = td.ThongTinNhanVien();
        }

        private void TuyenDungDaoTaoForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            td.DongKetNoi();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            td.XoaNVTuyenDung(gvTuyenDungDaoTao.Rows[0].Cells[0].Value.ToString());
            gvTuyenDungDaoTao.DataSource = td.ThongTinNhanVien();
        }

        private void gvTuyenDungDaoTao_SelectionChanged(object sender, EventArgs e)
        {
            if (gvTuyenDungDaoTao.SelectedRows.Count == 0)
                return;
            int i = gvTuyenDungDaoTao.SelectedRows[0].Index;
           
            txtMaNV.Text = gvTuyenDungDaoTao.Rows[i].Cells[0].Value.ToString();
            txtHoTen.Text = gvTuyenDungDaoTao.Rows[i].Cells[1].Value.ToString();
            cbPhai.Text = gvTuyenDungDaoTao.Rows[i].Cells[2].Value.ToString();
            dateTimePicker1.Value=DateTime.Parse(gvTuyenDungDaoTao.Rows[i].Cells[3].Value.ToString());
            txtDiaChi.Text = gvTuyenDungDaoTao.Rows[i].Cells[4].Value.ToString();
            txtQuanHuyen.Text=gvTuyenDungDaoTao.Rows[i].Cells[5].Value.ToString();
            cbTrinhDo.Text = gvTuyenDungDaoTao.Rows[i].Cells[6].Value.ToString();
            cblopdaotao.Text = gvTuyenDungDaoTao.Rows[i].Cells[7].Value.ToString();
        }
        private int TimKiem()
        {
            string chuoitim = txtMaNV.Text;
            int i = -1;
            foreach (DataGridViewRow dgv in gvTuyenDungDaoTao.Rows)
            {
                try
                {
                    i++;
                    if (string.Compare(chuoitim, dgv.Cells[0].Value.ToString()) == 0)
                        return i;
                }
                catch (Exception)
                {
                    MessageBox.Show("Ban nhap sai roi ! Nhap lai di");
                }
            }
            return -1;
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            int kq = TimKiem();
            if (kq == -1)
                MessageBox.Show("Khong tim thay", "Thong bao tim kiem");
            else
                gvTuyenDungDaoTao.Rows[kq].Selected = true;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            td.DongKetNoi();
            this.Close();
        }
    }
}
